package org.javaboy.vhr.service;

import org.javaboy.vhr.mapper.HrMapper;
import org.javaboy.vhr.mapper.OpLogMapper;
import org.javaboy.vhr.model.Hr;
import org.javaboy.vhr.model.OpLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class OpLogService {

    @Autowired
     OpLogMapper opLogMapper;

    @Autowired
    HrMapper hrMapper;

    /**
     * 查询所有
     * @return
     */
    public List<OpLog> getAllOpLog() {
        List<OpLog> allOpLog = opLogMapper.getAllOpLog();
        for (OpLog opLog : allOpLog) {
            System.out.println(opLog.getAdddate());
        }
        return opLogMapper.getAllOpLog();
    }

    /**
     * 添加数据
     * @return
     */
    public void addOplog(String method){

        //1.获取当前管理用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        Hr hr = hrMapper.selectByUsername(name);
        Integer hrId = hr.getId();

        //3.获取当前日期
        Date date = new Date();//获得系统时间
        Timestamp addDate = new Timestamp(date.getTime());

        //4.根据当前参数拼接具体操作
        String operate=null;
        if(method.contains("delete")){
            if(method.contains("Emp")){
                operate="员工删除";
            }else if(method.contains("employeeec")){
                operate="惩罚删除";
            }

        }

        if( method.contains("update") && method.contains("Emp")){
            operate="员工修改";
        }

        if(method.contains("add")){
            if(method.contains("Emp")){
                operate="员工添加";
            }else if(method.contains("employeeec")){
                operate="添加惩罚";
            }
        }
        //5.将三个参数进实体封装
        OpLog opLog = new OpLog();
        opLog.setHrid(hrId);
        opLog.setAdddate(addDate);
        opLog.setOperate(operate);


        //6.调用dao,将其添加到数据库中
        opLogMapper.insert(opLog);

    }

    /**
     * 删除
     * @param id
     * @return
     */
    public Integer deleteByPrimaryKey(Integer id) {
        return opLogMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    public OpLog selectByPrimaryKey(Integer id){
        return opLogMapper.selectByPrimaryKey(id);
    }


}
